import { Text, View, StyleSheet, TouchableWithoutFeedback } from 'react-native'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { asyncGetFriendList, SetFriendMsg } from "../../redux/features/appSlice"
import { pTx } from "../../utils/dimensions"
import { Cell } from '@pingtou/rn-vant'
import { formatFriendMsg } from "../../utils/formatFriendMsg"
import httpApi from "../../api"
import { showModal } from "../../components/rootSiblings/showModal"
import { useRealm, useQuery } from '@realm/react'
import { schemaName } from "../../realm"
import { useNavigation } from '@react-navigation/native';
import Realm from "realm"
import DeleteContact from "../../components/rootSiblings/deleteContact"

const FriendInfoMaterial = (props) => {
    const realm = useRealm();
    const params = props.route.params
    const userInfo = useQuery(schemaName.UserInfo, userInfo => {
        return userInfo.filtered('Current = true');
    })[0];
    const friendMsgLis = useQuery(schemaName.FriendMsgLis, friendMsg => {
        return friendMsg.filtered(`UserId == ${userInfo.Id} and F == ${params.Id}`)
    })
    const friendList = useQuery(schemaName.FriendList, friendMsg => {
        return friendMsg.filtered(`UserId == ${userInfo.Id} and F == ${params.Id}`)
    })
    const sendFriendDel = async () => {
        try {
            const res = await httpApi.sendFriendDel({ id: params.Id, msgType: 44 })
            if (res.code == 20) {
                realm.write(() => {
                    realm.delete(friendMsgLis);
                    realm.delete(friendList);
                })
                props.navigation.navigate('chat')
            }

        } catch (error) {
        }
    }
    return (
        <View style={styles.page}>
            <Cell.Group>
                <Cell title="昵称" center value={userInfo.NickName} />
                <Cell title="把他推荐给给朋友" center />
            </Cell.Group>
            <View style={styles.foot}>
                <TouchableWithoutFeedback
                    onPress={() => showModal(<DeleteContact operation={
                        [
                            { text: '删除联系人', onPress: () => sendFriendDel() },
                        ]
                    } />)}
                >
                    <View style={styles.footRow}>
                        <Text style={styles.sendMessage} >删除联系人</Text>
                    </View>
                </TouchableWithoutFeedback>
            </View>
        </View>
    )
}

const mapStateToProps = (state) => state.app

const mapDispatchToProps = {
    asyncGetFriendList,
    SetFriendMsg
}

export default connect(mapStateToProps, mapDispatchToProps)(FriendInfoMaterial)

const styles = StyleSheet.create({
    headerRightIcon: {
        paddingRight: pTx(15),
    },
    page: {
        flex: 1,
        backgroundColor: "#EDEDED"
    },
    foot: {
        marginTop: pTx(10),
        backgroundColor: "#ffffff"
    },
    footRow: {
        height: pTx(50),
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "center"
    },
    sendMessage: {
        marginLeft: pTx(3),
        fontSize: pTx(16),
        color: "#ee0a24",
        textAlign: "center"
    }
})